package com.zsr.example.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zsr.example.config.annotation.Logical;
import com.zsr.example.config.annotation.OptLog;
import com.zsr.example.config.annotation.RequiresPermissions;
import com.zsr.example.entity.EduArticle;
import com.zsr.example.entity.EduArticleCategory;
import com.zsr.example.service.EduArticleCategoryService;
import com.zsr.example.service.EduArticleService;
import com.zsr.example.util.CommonUtil;
import com.zsr.example.vo.ArticleReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.zsr.example.util.constants.SysLogConstants.DELETE;
import static com.zsr.example.util.constants.SysLogConstants.UPDATE;

/**
 * <p>
 * 文章表 前端控制器
 * </p>
 *
 * @author zsr
 * @since 2022-01-05
 */
@RestController
@RequestMapping("/article")
public class EduArticleController {
    @Autowired
    private EduArticleService articleService;

    @Autowired
    private EduArticleCategoryService articleCategoryService;


    @RequiresPermissions("article:list")
    @GetMapping("/getArticleCategory")
    public JSONObject getArticleCategory(){
        List<EduArticleCategory> categoryList = articleCategoryService.list(null);
        return CommonUtil.successJson(categoryList);
    }
    
    @OptLog(optType = UPDATE,optDesc = "更新图文")
    @RequiresPermissions("article:update")
    @PostMapping("/saveOrUpdateCategory")
    public JSONObject saveOrUpdateCategory(@RequestBody EduArticleCategory category){
        articleCategoryService.saveOrUpdate(category);
        return CommonUtil.successJson();
    }
    @OptLog(optType = DELETE,optDesc = "删除图文")
    @RequiresPermissions("article:delete")
    @GetMapping("/delCategory/{id}")
    public JSONObject delCategory(@PathVariable String id){
        articleCategoryService.removeById(id);
        return CommonUtil.successJson();
    }

    //分割线

    /**
     * 查询文章列表
     */
    @RequiresPermissions("article:list")
    @PostMapping("/pageListArticleCondition/{current}/{limit}")
    public JSONObject listArticle(@PathVariable long current,
                                  @PathVariable long limit,
                                  @RequestBody(required = false) ArticleReq articleReq) {
        Page<EduArticle> page = new Page<>(current, limit);

        articleService.pageQuery(page, articleReq);

        return CommonUtil.successPage(page.getTotal(), page.getRecords());
    }

    /**
     * 获取文章信息
     * @param id
     * @return
     */
    @RequiresPermissions("article:list")
    @GetMapping("/getArticle/{id}")
    public JSONObject getArticle(@PathVariable String id){
        EduArticle article = articleService.getById(id);

        return CommonUtil.successJson(article);
    }

    /**
     * 新增文章
     */
    @RequiresPermissions("article:delete")
    @DeleteMapping("/{id}")
    public JSONObject delArticle(@PathVariable String id) {
        boolean result = articleService.removeById(id);
        return result? CommonUtil.successJson():CommonUtil.errorJson();
    }

    /**
     * 保存or修改文章
     */
    @RequiresPermissions(value = {"article:add","article:update"},logical = Logical.OR)
    @PostMapping("/updateArticle")
    public JSONObject updateArticle(@RequestBody EduArticle eduArticle) {
        boolean b = articleService.saveOrUpdate(eduArticle);
        return b ? CommonUtil.successJson() : CommonUtil.errorJson();
    }
}

